﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAOChiTietMonAn:DataProvider
    {
        public static DataTable getAll(string mamonan)
        {
            string select = "select mamonan,chitietmonan.manvl,nguyenvatlieu.tennvl,nguyenvatlieu.maloai from chitietmonan left join nguyenvatlieu on nguyenvatlieu.manvl = chitietmonan.manvl where mamonan = @mamonan";
            var da = new SqlDataAdapter(select, KetNoi());
            da.SelectCommand.Parameters.AddWithValue("@mamonan", mamonan);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DongKetNoi();
            return dt;
        }        

        public static int save(DataTable dt,string mamonan)
        {
            string delete = "delete from chitietmonan where mamonan=@mamonan";
            var cmd = new SqlCommand(delete, KetNoi());
            cmd.Parameters.AddWithValue("@mamonan", mamonan);
            cmd.ExecuteNonQuery();

            string select = "select * from chitietmonan";
            var da = new SqlDataAdapter(select, KetNoi());
            new SqlCommandBuilder(da);            
            DataTable mydt = new DataTable();
            da.FillSchema(mydt, SchemaType.Source);            

            DataRow dr;
            int length = dt.Rows.Count;
            for (int i = 0; i < length; i++)
			{
                dr = mydt.NewRow();
                dr["MaNVL"] = dt.Rows[i]["MaNVL"].ToString();
                dr["MaMonAn"] = mamonan;
                mydt.Rows.Add(dr);
			}
            
            int kq = da.Update(mydt);
            DongKetNoi();
            return kq;
        }
    }
}
